# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=openocd-esp32
pkgver=0_git20230921
_commit=a23a2d4f82f848a9e76b0b79c4b26782f26b11ed
pkgrel=4
pkgdesc="Fork of OpenOCD with ESP32 support"
url="https://github.com/espressif/openocd-esp32"
arch="all"
license="GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-only"
makedepends="
	autoconf
	automake
	hidapi-dev
	jimtcl-dev
	libftdi1-dev
	libjaylink-dev
	libtool
	libusb-dev
	"
subpackages="$pkgname-dev $pkgname-doc $pkgname-udev-rules:udev_rules"
source="$pkgname-$pkgver.tar.gz::https://github.com/espressif/openocd-esp32/archive/$_commit.tar.gz
	fix-jimtcl-link.patch"
builddir="$srcdir/openocd-esp32-$_commit"

##
# XXX: Remove this aport when changes are upstreamed.
##

prepare() {
	default_prepare
	./bootstrap nosubmodule
}

build() {
	# --datadir:
	# avoid conflicts with upstream openocd to allow parallel installations
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--datadir=/usr/share/$pkgname \
		--localstatedir=/var \
		--disable-werror \
		--disable-internal-jimtcl \
		--disable-internal-libjaylink
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	install -Dm644 contrib/60-openocd.rules \
		"$pkgdir"/lib/udev/rules.d/60-$pkgname.rules

	# allow parallel install with upstream openocd
	mv "$pkgdir"/usr/bin/openocd "$pkgdir"/usr/bin/$pkgname
	mv "$pkgdir"/usr/share/man/man1/openocd.1 \
		"$pkgdir"/usr/share/man/man1/openocd-esp32.1
	sed -e 's/openocd/openocd-esp32/g' -i \
		"$pkgdir"/usr/share/man/man1/openocd-esp32.1
}

udev_rules() {
	pkgdesc="udev rules for $pkgname"
	depends="!openocd-udev-rules"
	amove lib/udev/rules.d/60-$pkgname.rules
}

sha512sums="
cb884616a7547c30776629923d6a599f5702506a96a47076bbdfbe0a550b560a1277e751a8a6cd3e6bc7f1894604c1899d9416c731fa91ce71a95049f3cc8479  openocd-esp32-0_git20230921.tar.gz
6fd2fbe11917bb7f181ba67812f63e75d51bcd19e4df6a7e22e1abb8f36a4f4bdc9202e815f560476ec184d54d84df8ae4dceb17f2429b9dc1d9503804dacb03  fix-jimtcl-link.patch
"
